package com.level2app.ui.welcome;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;

import com.level2app.R;
import com.level2app.base.BaseActivity;
import com.level2app.databinding.ActivityReferralCodeBinding;
import com.level2app.ui.welcome.view.ReferralCodeViewModel;
import com.level2app.utils.JumpIntent;
import com.level2app.utils.KeyboardUtils;
import com.level2app.utils.ToastHelper;

/**
 * 输入推荐码
 */
public class ReferralCodeActivity extends BaseActivity<ActivityReferralCodeBinding, ReferralCodeViewModel> {


    @Override
    protected ReferralCodeViewModel bindModel() {
        return getViewModel(this, ReferralCodeViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar();
        binding.setViewModel(viewModel);
    }

    @Override
    protected void initClick() {
        super.initClick();
        viewModel.mInputData.observe(this, sting -> {
            //输入
            if (TextUtils.isEmpty(sting)) {
                binding.inputCodeEt.setBackgroundResource(R.drawable.shape_stroke_e4e4e4_99dp);
                binding.confirmBtn.setBackgroundResource(R.drawable.shape_btn_bg_def);
                binding.confirmBtn.setTextColor(getColor(R.color.color_9ca3af));
                binding.confirmBtn.setEnabled(false);
            } else {
                binding.inputCodeEt.setBackgroundResource(R.drawable.shape_stroke_000000_99dp);
                binding.confirmBtn.setBackgroundResource(R.drawable.shape_btn_bg);
                binding.confirmBtn.setTextColor(Color.WHITE);
                binding.confirmBtn.setEnabled(true);
            }
        });
        viewModel.onDelayClick(binding.confirmBtn, o -> {
            //确认
            showLoadingDialog();
            viewModel.checkReferralCode();

//            //失败
//            binding.errorLl.setVisibility(View.VISIBLE);
//            //错误提示
//            if (true) {
//                //超过100人限制
//                binding.errorTv.setText(getString(R.string.code_maximum_invitations));
//            } else {
//                //邀请码失效
//                binding.errorTv.setText(getString(R.string.invalid_code_again));
//            }
//            binding.inputCodeEt.setBackgroundResource(R.drawable.shape_stroke_ff2d55_99dp);
//            //成功
//            JumpIntent.jump(this, ScanFinishActivity.class);
        });
        viewModel.checkReferralCodeData.observe(this, bean -> {
            dismissLoadingDialog();
            JumpIntent.jump(this, ScanFinishActivity.class);
        });
        viewModel.mErrorData.observe(this, s -> {
            dismissLoadingDialog();
            //TODO：error在输入框下提示

        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            //返回具有焦点的当前视图
            View v = getCurrentFocus();
            if (KeyboardUtils.isShouldHideInput(v, ev)) {
                KeyboardUtils.hideSoftInput(this);
            }
        }
        return super.dispatchTouchEvent(ev);
    }
}